package api

import (
	"company_website/src/global"
	"company_website/src/model"
	"company_website/src/model/common"
	"company_website/src/model/request"
	"company_website/src/model/response"
	"company_website/src/server"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Client struct{}

// GetClientMessage 分页获取客户消息
func (cli Client) GetClientMessage(c *gin.Context) {
	reqInfo := &common.GetPageInfo{}
	if err := c.ShouldBindJSON(&reqInfo); err != nil {
		global.GVL_LOG.Error("接受参数失败", err)
		response.FailWithMessage("接受参数失败", c)
		return
	}
	//到数据库中查找数据
	resInfo, err := server.ClientServer.GetAllClientInfoPage(reqInfo)
	if err != nil {
		global.GVL_LOG.Error("获取用户列表失败", err)
		response.FailWithMessage("获取用户列表失败", c)
		return
	}
	response.OkWithData(resInfo, c)
}

// ModifyClientStatus 更改用户状态
func (cli Client) ModifyClientStatus(c *gin.Context) {
	var client request.GetClientId
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMessage("获取用户失败", c)
		return
	}
	user := &model.User{
		Id:     client.Id,
		Status: client.Status,
	}
	err = global.GVL_DB.Model(&model.User{}).Where("id = ?", user.Id).Updates(&user).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			response.FailWithMessage("未查询到该Id的用户", c)
			return
		}
		response.FailWithMessage("更改失败", c)
		return
	}
	response.OkWithData("更改成功", c)
}
